package com.cloud.poetry.queue.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author mengxj
 */
@Slf4j
@Configuration
public class RabbitmqConfig {

    @Bean("rabbitTemplate")
    public RabbitTemplate defaultRabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if(ack){
                log.info("pushing to rabbitmq success!");
            }else{
                log.info("pushing to rabbitmq error!");
            }
        });
//        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exc, key) ->
//                CommonUtil.execute(() -> this.sysLogService.insertReturnCallback(message, replyCode, replyText, exc, key)));
        return rabbitTemplate;
    }

    @Bean("batchQueueRabbitListener100")
    public SimpleRabbitListenerContainerFactory batchQueueRabbitListenerContainerFactory100(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setBatchListener(true);
        factory.setConsumerBatchEnabled(true);
        factory.setBatchSize(100);
        return factory;
    }

    @Bean("batchQueueRabbitListener1000")
    public SimpleRabbitListenerContainerFactory batchQueueRabbitListenerContainerFactory1000(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setBatchListener(true);
        factory.setConsumerBatchEnabled(true);
        factory.setBatchSize(1000);
        return factory;
    }
}
